package cn.schoolwow.quickdao.module.database.dml.json.flow.common;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.ArrayList;
import java.util.List;

public class SetColumnNameListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        String tableName = (String) flowContext.checkData("tableName");

        List<String> columnNameList = new ArrayList<>();
        if(manipulationOption.partColumnSet.isEmpty()){
            Entity entity = quickDAOConfig.getDatabaseEntityByTableName(tableName);
            for(Property property:entity.properties){
                columnNameList.add(property.column);
            }
            if(null!=entity.id&&entity.id.strategy.equals(IdStrategy.AutoIncrement)){
                columnNameList.remove(entity.id.column);
            }
        }else{
            columnNameList.addAll(manipulationOption.partColumnSet);
        }
        flowContext.putTemporaryData("columnNameList", columnNameList);
    }

    @Override
    public String name() {
        return "设置字段列表";
    }
}
